<?php
/**
 * Models Base Class
 * User: zng
 * Date: 2019/9/11
 * Time: 09:11
 */
namespace app\admin\model\admin;

use app\admin\model\Models;

class PermissionCategory extends Models
{

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $updateTime = false;

    /**
     * 设置时间戳格式
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 设置状态获取器
     * @return string
     */
    protected function getStatusTextAttr( $value, $data )
    {
        $status = config('field.permission_category.status');
        return $status[$data['status']]??'暂无';
    }

    /**
     * 登录查询
     * @return void
     */
    public function scopePermissionCategoryByUserId( $query, int $user_id )
    {
        $query->alias('permission_category')
            ->leftJoin('permission', 'permission_category.id = permission.category_id')
            ->leftJoin('role_permission', 'permission.id = role_permission.permission_id')
            ->leftJoin('admin_role', 'role_permission.role_id = admin_role.role_id')
            ->where('admin_role.user_id', $user_id)->group('permission_category.id');
    }

    /**
     * 查询状态
     * @return void
     */
    public function scopeStatus( $query, int $status = 1 )
    {
        $query->where('status', $status);
    }

}